package com.azhon.entry.slice;


import com.azhon.appupdate.config.UpdateConfiguration;
import com.azhon.appupdate.listener.OnButtonClickListener;
import com.azhon.appupdate.listener.OnDownloadListenerAdapter;
import com.azhon.appupdate.manager.DownloadManager;
import com.azhon.appupdate.utils.Log;
import com.azhon.entry.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ProgressBar;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;

import java.io.File;

import static ohos.agp.components.DependentLayout.LayoutConfig.MATCH_CONTENT;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener, OnButtonClickListener {

    private static final int MAX = 100;
    private static final int WIDTH = 1000;
    private DownloadManager manager;
    private ProgressBar progressBar;
    private String url = "https://imtt.dd.qq.com/16891/apk/FA48766BA12A41A1D619CB4B152889C6.apk?fsname=com.estrongs.android.pop_4.2.3.3_10089.apk&csr=1bbd";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        findComponentById(ResourceTable.Id_btn_1).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_2).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_3).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_4).setClickedListener(this);
        progressBar = (ProgressBar) findComponentById(ResourceTable.Id_progress);

    }

    public void onClick(Component v) {
        switch (v.getId()) {
            case ResourceTable.Id_btn_1:
                startUpdate1();
                break;
            case ResourceTable.Id_btn_2:
                startUpdate2();
                break;
            case ResourceTable.Id_btn_3:
                startUpdate3();
                break;
            case ResourceTable.Id_btn_4:
                if (manager != null) {
                    manager.cancel();
                }
                break;
            default:
                break;
        }
    }

    private void startUpdate1() {
        DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_dialog, null, false);
        new CommonDialog(getContext())
                .setContentCustomComponent(layout)
                .setButton(1, "升级", new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog iDialog, int i) {
                        progressBar.setProgressValue(0);
                        progressBar.setProgressHintText(0 + "%");
                        manager = DownloadManager.getInstance(getContext());
                        manager.setApkName("ESFileExplorer.apk")
                                .setApkUrl(url)
                                .setSmallIcon(ResourceTable.Media_ic_launcher)
                                .download();
                        iDialog.destroy();
                    }
                })
                .setSize(WIDTH, MATCH_CONTENT)
                .show();
    }

    private void startUpdate2() {
        progressBar.setProgressValue(0);
        progressBar.setProgressHintText(0 + "%");
        manager = DownloadManager.getInstance(getContext());
        manager.setApkName("ESFileExplorer.apk")
                .setApkUrl(url)
                .setSmallIcon(ResourceTable.Media_ic_launcher)
                .download();
    }

    private void startUpdate3() {
        UpdateConfiguration configuration = new UpdateConfiguration()
                //输出错误日志
                .setEnableLog(true)
                //设置自定义的下载
                //.setHttpManager()
                //下载完成自动跳动安装页面
                .setJumpInstallPage(true)
                //设置对话框背景图片 (图片规范参照demo中的示例图)
//                .setDialogImage(ResourceTable.Media_ic_dialog)
                //设置按钮的颜色
//                .setDialogButtonColor(RgbPalette.parse("#E743DA"))
                //设置对话框强制更新时进度条和文字的颜色
//                .setDialogProgressBarColor(RgbPalette.parse("#E743DA"))
                //设置按钮的文字颜色
                .setDialogButtonTextColor(Color.getIntColor("WHITE"))
                //设置是否显示通知栏进度
                .setShowNotification(true)
                //设置是否提示后台下载toast
                .setShowBgdToast(false)
                //设置是否上报数据
                .setUsePlatform(true)
                //设置强制更新
                .setForcedUpgrade(false)
                //设置对话框按钮的点击监听
                .setButtonClickListener(this)
                //设置下载过程的监听
                .setOnDownloadListener(listenerAdapter);

        manager = DownloadManager.getInstance(this);
        manager.setApkName("ESFileExplorer.apk")
                .setApkUrl(url)
                .setSmallIcon(ResourceTable.Media_ic_launcher)
                .setShowNewerToast(true)
                .setConfiguration(configuration)
                .setApkVersionCode(2000000)
                .setApkVersionName("2.1.8")
                .setApkSize("20.4")
                .setApkDescription(getString(ResourceTable.String_dialog_msg))
//                .setApkMD5("34F19396A08E6BC14D866D5D440E2C997CE8FB33BF0A41ED312A973AE6F1866E")
                .download();
    }

    private OnDownloadListenerAdapter listenerAdapter = new OnDownloadListenerAdapter() {
        /**
         * 下载中
         *
         * @param progress 当前进度
         */
        @Override
        public void downloading(int progress) {
            progressBar.setProgressValue(progress);
            progressBar.setProgressHintText(progress + "%");
        }
        public void done(File apk) {
            progressBar.setProgressValue(MAX);
            progressBar.setProgressHintText(MAX + "%");
        }
    };

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onButtonClick(int id) {
        Log.e("TAG", String.valueOf(id));
    }
}
